/**
 * 版权所有(C)，中国银联股份有限公司，2002-2013，所有权利保留。
 * 
 * 项目名：	simple-web
 * 文件名：	MenuAction.java
 * 模块说明：	
 * 修改历史：
 * 2013-10-31 - Administrator - 创建。
 */
package com.aeky.simple.action.system;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.aeky.simple.action.BaseAction;
import com.aeky.simple.cache.CacheManager;
import com.aeky.simple.entity.FunctionInfo;
import com.aeky.simple.entity.SystemInfo;
import com.aeky.simple.vo.tree.TreeNode;

/**
 * @author wanghui
 * 
 */
public class MenuAction extends BaseAction {

    private static final long serialVersionUID = -3120397262818831823L;

    /** 菜单节点ID(初始化为sysCode,其它为funcCode) */
    private String id;

    /**
     * 初始化菜单
     * 
     * @return
     * @throws Exception
     */
    public String initMenu() throws Exception {
        List<TreeNode> treeList = new ArrayList<TreeNode>();
        if (StringUtils.isEmpty(id)) {
            treeList = this.initSysInfoTreeNode();
        } else {
            String[] ids = id.split("-");
            String sysCode = ids[0];
            String funcCode = sysCode;
            if (ids.length == 2) {
                funcCode = ids[1];
            }
            treeList = this.getSubTreeNode(sysCode, funcCode);
        }
        ajaxResult = this.getJsonFromObject(treeList);
        return AJAX_RESULT;
    }

    /**
     * 根据系统编号、功能权限编号获取菜单子节点
     * 
     * @param sysCode
     * @param funcCode
     * @return
     */
    private List<TreeNode> getSubTreeNode(String sysCode, String funcCode) {
        List<TreeNode> subTreeNodeList = new ArrayList<TreeNode>();
        Map<String, LinkedHashMap<String, FunctionInfo>> sysFuncMap = CacheManager
                        .getSystemFunctionInfoCache();
        if (sysFuncMap != null && !sysFuncMap.isEmpty()) {
            Map<String, FunctionInfo> funcMap = sysFuncMap.get(sysCode);
            if (funcMap != null && !funcMap.isEmpty()) {
                for (Map.Entry<String, FunctionInfo> entry : funcMap.entrySet()) {
                    FunctionInfo fi = entry.getValue();
                    if (funcCode.equals(fi.getParentFuncCode()) && !"2".equals(fi.getFuncType())) {
                        TreeNode tn = new TreeNode();
                        tn.setId(sysCode + "-" + fi.getFuncCode());
                        tn.setText(fi.getFuncName());
                        if (isLeaf(sysCode, fi.getFuncCode())) {
                            tn.getAttributes().setUrl(fi.getFuncUrl());
                            tn.setState("open");
                        }
                        subTreeNodeList.add(tn);
                    }
                }
            }
        }
        return subTreeNodeList;
    }

    /**
     * 初始化系统节点TreeNode
     * 
     * @return
     */
    private List<TreeNode> initSysInfoTreeNode() {
        List<TreeNode> sysInfoTreeNodeList = new ArrayList<TreeNode>();
        Map<String, SystemInfo> sysInfoMap = CacheManager.getSystemInfoCache();
        if (sysInfoMap != null && !sysInfoMap.isEmpty()) {
            for (Map.Entry<String, SystemInfo> entry : sysInfoMap.entrySet()) {
                SystemInfo si = entry.getValue();
                TreeNode tn = new TreeNode();
                tn.setId(si.getSysCode());
                tn.setText(si.getSysName());
                sysInfoTreeNodeList.add(tn);
            }
        }
        return sysInfoTreeNodeList;
    }

    /**
     * 判断某节点是否为叶子节点
     * 
     * @param sysCode
     * @param funcCode
     * @return
     */
    private Boolean isLeaf(String sysCode, String funcCode) {
        Boolean flag = Boolean.TRUE;
        Map<String, FunctionInfo> map = CacheManager.getSystemFunctionInfoCache().get(sysCode);
        for (Map.Entry<String, FunctionInfo> entry : map.entrySet()) {
            FunctionInfo fi = entry.getValue();
            if (funcCode.equals(fi.getParentFuncCode()) && !"2".equals(fi.getFuncType())) {
                flag = Boolean.FALSE;
                break;
            }
        }
        return flag;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
